package com.geovis.emergency.spd.biz.basic.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.basic.mapper.BasicMaterialMapper;
import com.geovis.emergency.spd.biz.basic.service.IBasicMaterialService;
import com.geovis.emergency.spd.biz.system.service.ISystemFileService;
import com.geovis.emergency.spd.entity.basic.entity.BasicMaterial;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicMaterialDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicMaterialQueryDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicMaterialUploadDTO;
import com.geovis.emergency.spd.entity.system.entity.SystemFile;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemFileUploadDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 基础模块-素材管理 服务实现类
 * </p>
 *
 * @author 王响
 * @since 2022-03-31
 */

@Service
@RequiredArgsConstructor
@Slf4j
public class BasicMaterialServiceImpl extends ServiceImpl<BasicMaterialMapper, BasicMaterial> implements IBasicMaterialService {


    private final ISystemFileService systemFileService;

    @Override
    public PageResult<BasicMaterialDTO> listPage(PageParam<BasicMaterialQueryDTO> pageParam) {
        IPage<BasicMaterialDTO> page = pageParam.buildPage();
        QueryWrapper<Object> wrapper = buildWrapper(pageParam.getQuery());
        baseMapper.selectByPage(page, wrapper);
        return new PageResult<BasicMaterialDTO>(page);
    }

    /**
     * 构建查询条件
     *
     * @param queryDTO
     * @return
     */
    private QueryWrapper<Object> buildWrapper(BasicMaterialQueryDTO queryDTO) {
        QueryWrapper<Object> wrapper = Wrappers.query();
        if (ObjectUtil.isEmpty(queryDTO)) {
            return wrapper;
        }
        wrapper.like(ObjectUtil.isNotEmpty(queryDTO.getTag()), "a.tag", queryDTO.getTag());
        wrapper.likeRight(ObjectUtil.isNotEmpty(queryDTO.getTag()), "a.tag", queryDTO.getTag());
        return wrapper;
    }

    @Override
    public void upload(BasicMaterialUploadDTO uploadDTO) {

        SystemFileUploadDTO systemFileUploadDTO = BeanUtil.toBean(uploadDTO, SystemFileUploadDTO.class);

        // 1、上传
        SystemFile systemFile = systemFileService.upload(systemFileUploadDTO);

        // 2、保存本数据
        BasicMaterial basicMaterial = BeanUtil.toBean(uploadDTO, BasicMaterial.class);
        basicMaterial.setSystemFileId(systemFile.getId());
        this.save(basicMaterial);
    }

    @Override
    public void removeByIdList(Set<String> idList) {
        List<BasicMaterial> list = this.listByIds(idList);
        if (ObjectUtil.isEmpty(list)) {
            return;
        }
        Set<String> systemFileIdSet = list.stream().map(BasicMaterial::getSystemFileId).collect(Collectors.toSet());
        systemFileService.removeByIdList(systemFileIdSet);
        this.removeByIds(idList);
    }
}
